package route

import (
	"app/api"
	"app/middleware"

	"github.com/gin-gonic/gin"
)

// 路由配置
func NewRouter() *gin.Engine {
	r := gin.Default()       // 生成应用程序实例
	r.Use(middleware.Cors()) // 允许跨域访问
	v1 := r.Group("v1")
	{
		// 用户操作
		v1.POST("user/register", api.UserRegister)
		v1.POST("user/login", api.UserLogin)
		// 需要jwt鉴权
		authed := v1.Group("/")
		authed.Use(middleware.JWT())
		{
			// 任务操作
			authed.GET("tasks", api.ListTasks)
			authed.POST("task", api.CreateTask)
			authed.GET("task/:id", api.ShowTask)
			authed.DELETE("task/:id", api.DeleteTask)
			authed.PUT("task/:id", api.UpdateTask)
			authed.POST("search", api.SearchTasks)
		}
	}
	return r
}
